package cn.qiyu5522.initdata;

import cn.qiyu5522.constant.SystemConsKeys;
import cn.qiyu5522.domain.entity.Article;
import cn.qiyu5522.mapper.ArticleMapper;
import cn.qiyu5522.utils.RedisCache;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * ClassName: ViewCountRunner
 * Package: cn.qiyu5522.init
 * Description:
 *
 * @Author: Tonkey
 * @Create：2024/4/15-15:47
 * @Version: v1.0
 */

@Component
public class ViewCountRunner implements CommandLineRunner {

    @Resource
    private RedisCache redisCache;

    @Resource
    private ArticleMapper articleMapper;

    @Override
    public void run(String... args) throws Exception {
        // 1. 将博客的浏览量信息，存入redis
        // 1.1 查询博客的流浪量
        List<Article> articles = articleMapper.selectList(null);
        Map<String, Integer> viewCountMap = articles.stream().collect(Collectors.toMap(
                article -> article.getId().toString(), article -> article.getViewCount().intValue()
        ));
        // 1.2 存入redis
        redisCache.setCacheMap(SystemConsKeys.ARTICLE_VIEW_COUNT,viewCountMap);

    }
}
